package com.AAL.DAO;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.ITransferingRequestDAO;
import com.AAL.Model.RequestingDetailsForTransfering;
import com.AAL.Model.SourcingRequest;
import com.AAL.Model.StatusOfRequest;
import com.AAL.Model.TransferingRequest;
import com.AAL.Utils.HibernateUtil;

public class TransferingRequestDAO implements ITransferingRequestDAO {

	@Override
	public TransferingRequest saveAndUpdateTransferingRequest(
			TransferingRequest transferingRequest) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(transferingRequest);
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		session.getTransaction().commit();
		return transferingRequest;
	}

	@Override
	public TransferingRequest deleteTransferingRequest(int transferingRequest) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TransferingRequest getTransferingRequest(String transferingRequestID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		TransferingRequest transferingRequest = (TransferingRequest) session
				.load(TransferingRequest.class, transferingRequestID);
		return transferingRequest;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TransferingRequest> getAllTransferingRequests() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<TransferingRequest> listTransferingRequest = null;
		try {

			listTransferingRequest = (List<TransferingRequest>) session
					.createQuery("from TransferingRequest").list();
			// System.out.println("thuc hien dao!");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return listTransferingRequest;
	}

	// Phatdt add
	@SuppressWarnings({ "unchecked", "deprecation", "null" })
	public List<TransferingRequest> getAllTransferingRequestsSearch(
			String transferingRequestIDs, String statusOfTransferingRequestIDs,
			Timestamp writingDateStart, Timestamp writingDateEnd,
			Timestamp requirementDateStart, Timestamp requirementDateEnd) {
		System.out.println("goi ham searchDAO");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<TransferingRequest> listTransferingRequest = null;
		String SQL = null;
		if(transferingRequestIDs.equals("")){ 
		if(statusOfTransferingRequestIDs.equals("0")){
		 SQL="from TransferingRequest as T where (T.writingDate between '"+writingDateStart+"' and '"+writingDateEnd+"') and (T.requirementDate between '"+requirementDateStart+"' and '"+requirementDateEnd+"')"; }
		else {
		 SQL="from TransferingRequest as T where (T.statusOfTransferingRequest.statusOfTransferingRequestID='"+statusOfTransferingRequestIDs+"') and (T.writingDate between '"+writingDateStart+"' and '"+writingDateEnd+"') and (T.requirementDate between '"+requirementDateStart+"' and '"+requirementDateEnd+"')"; 
		  }		
		}
		else{
			if(statusOfTransferingRequestIDs.equals("0")){
				 SQL="from TransferingRequest as T where (T.transferingRequestID like '"+statusOfTransferingRequestIDs+"') and (T.writingDate between '"+writingDateStart+"' and '"+writingDateEnd+"') and (T.requirementDate between '"+requirementDateStart+"' and '"+requirementDateEnd+"')"; }
				else {
				 SQL="from TransferingRequest as T where (T.transferingRequestID like '"+statusOfTransferingRequestIDs+"') and (T.statusOfTransferingRequest.statusOfTransferingRequestID='"+statusOfTransferingRequestIDs+"') and (T.writingDate between '"+writingDateStart+"' and '"+writingDateEnd+"') and (T.requirementDate between '"+requirementDateStart+"' and '"+requirementDateEnd+"')"; 
				  }
		}
		try {

			listTransferingRequest = (List<TransferingRequest>) session
					.createQuery(SQL)										
					.list();
			System.out.println("thuc hien dao!");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return listTransferingRequest;

	}

	@Override
	public TransferingRequest getTransferingRequest(int transferingRequest) {
		// TODO Auto-generated method stub
		return null;
	}
//phat

	@Override
	public TransferingRequest saveAndUpdateTransferingRequest(
			TransferingRequest transferingRequest,
			List<SourcingRequest> lstsourcingRequests) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		try {
			session.saveOrUpdate(transferingRequest);
			 List<RequestingDetailsForTransfering> listRequestingDetailsForTransferingsTemp= transferingRequest.getRequestingDetailsForTransfering();
			 for(RequestingDetailsForTransfering item: listRequestingDetailsForTransferingsTemp){
				 item.setTransferingRequest(transferingRequest);
				 session.saveOrUpdate(item);
			 }
			 for(SourcingRequest request: lstsourcingRequests){
				 
				 StatusOfRequestDAO statusOfRequestDAO = new StatusOfRequestDAO();
				 StatusOfRequest statusOfRequestTemp=statusOfRequestDAO.getStatusOfRequest(5);
				 request.setStatusOfRequest(statusOfRequestTemp);
				 session.saveOrUpdate(statusOfRequestTemp);
				 
			 }
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Loi:"+ e.getMessage());
			session.getTransaction().rollback();
			return null;
		}
		session.getTransaction().commit();
		return transferingRequest;
	}
	//phat end

}
